package com.xunyin.modules.mtdianping.rest;

import com.xunyin.annotation.AnonymousAccess;
import com.xunyin.dianping.DianpingCouponService;
import com.xunyin.modules.mtdianpinggroup.domain.MtDianpingGroup;
import com.xunyin.modules.security.security.TokenUtil;
import com.xunyin.modules.security.security.vo.JwtUser;
import com.xunyin.utils.StringUtil;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 美团点评团购套餐类controller
 * <p>
 * Created by xunyin
 * CopyRight 讯音云商 https://www.tonetn.com
 */
@Slf4j
@AllArgsConstructor
@Api(tags = "共享空间:美团点评团购套餐")
@RestController
@RequestMapping("api/dianping")
public class BackendDianpingController {

    private final DianpingCouponService dianpingCouponService;
    private final TokenUtil tokenUtil;

/*    @GetMapping("/callBackUrl")
    @ApiOperation(value = "美团点评团购套餐",notes = "美团点评团购套餐",response = YxUserQueryVo.class)
    @CrossOrigin
    public void callBackUrl(HttpServletRequest request, @RequestParam("state") String state, @RequestParam("auth_code") String auth_code){
        log.info("callBackUrl state:"+state+",auth_code:"+auth_code);
    }*/

    /**
     * 点评美团门店套餐映射
     *
     * @return
     */
    @GetMapping(value = "/syncGroupInfor")
    @AnonymousAccess
    public ResponseEntity syncGroupInfor(HttpServletRequest request, @RequestParam("storeId") Integer storeId) throws IOException, NoSuchAlgorithmException {
        dianpingCouponService.syncGroupInfor(storeId);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    /**
     * 根据本地门店Id,获取点评美团同步的团购套餐列表
     */
    @RequestMapping(value = "/dianpingGroupList", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseEntity<Map<String, Object>> dianpingGroupList(HttpServletRequest request) {

        Integer storeId = (request.getParameter("storeId") == null || StringUtil.isEmpty(request.getParameter("storeId"))) ? 0 : Integer.parseInt(request.getParameter("storeId"));
        MtDianpingGroup mtDianpingGroup = new MtDianpingGroup();
//        mtDianpingGroup.setStoreId(storeId);
        JwtUser user = tokenUtil.getJwtUser(tokenUtil.getToken(request));
        List<Integer> storeIdList = null;
        if (!user.getRoleIdList().contains(Long.valueOf(1L))) {
            storeIdList = user.getStoreIdList();
        }
        List<MtDianpingGroup> mtDianpingGroupList = dianpingCouponService.getDianpingGroupList(mtDianpingGroup, storeIdList);
        Map<String, Object> result = new HashMap<>();
        result.put("dianpingGroupList", mtDianpingGroupList);
        return new ResponseEntity<>(result, HttpStatus.OK);

    }

}
